/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cadastros;

import dao.DaoConvenio;
import modelo.Cidade;
import modelo.Convenio;
import util.mensagem;
import util.pesquisarCidade;

/**
 *
 * @author fumio
 */
public class CadConvenio extends javax.swing.JDialog {

    private DaoConvenio dao = new DaoConvenio();
    private Cidade cidade = null;

    /**
     * Creates new form CadConvenio
     */
    public CadConvenio(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        edtObs = new javax.swing.JTextArea();
        edtTelefone = new javax.swing.JTextField();
        edtRepresentante = new javax.swing.JTextField();
        edtCNPJ = new javax.swing.JTextField();
        edtEndereco = new javax.swing.JTextField();
        edtNome = new javax.swing.JTextField();
        edtCodigo = new javax.swing.JTextField();
        edtNo = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        edtIE = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        edtCelular = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        edtCodCidade = new javax.swing.JTextField();
        btnPesquisar = new javax.swing.JButton();
        edtCidade = new javax.swing.JTextField();
        edtUF = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        btnGravar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel3.setBorder(javax.swing.BorderFactory.createCompoundBorder(null, javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));
        jPanel3.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel1.setText("Obs:");
        jLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        jPanel3.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 350, -1, -1));

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel2.setText("Código:");
        jLabel2.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        jPanel3.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 20, -1, -1));

        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel7.setText("Nome:");
        jLabel7.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        jPanel3.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 60, -1, -1));

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel3.setText("Endereco:");
        jLabel3.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        jPanel3.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 110, -1, -1));

        jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel9.setText("CNPJ:");
        jLabel9.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        jPanel3.add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 180, -1, 20));

        jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel8.setText("Representante:");
        jLabel8.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        jPanel3.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 220, -1, -1));

        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel6.setText("Telefone:");
        jLabel6.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        jPanel3.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 260, -1, -1));

        edtObs.setColumns(20);
        edtObs.setRows(5);
        jScrollPane1.setViewportView(edtObs);

        jPanel3.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 290, 360, -1));
        jPanel3.add(edtTelefone, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 250, 160, -1));
        jPanel3.add(edtRepresentante, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 210, 360, -1));
        jPanel3.add(edtCNPJ, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 175, 190, -1));
        jPanel3.add(edtEndereco, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 100, 280, -1));
        jPanel3.add(edtNome, new org.netbeans.lib.awtextra.AbsoluteConstraints(132, 50, 360, -1));

        edtCodigo.setEnabled(false);
        jPanel3.add(edtCodigo, new org.netbeans.lib.awtextra.AbsoluteConstraints(132, 13, 60, -1));

        edtNo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                edtNoActionPerformed(evt);
            }
        });
        jPanel3.add(edtNo, new org.netbeans.lib.awtextra.AbsoluteConstraints(440, 100, 50, -1));

        jLabel5.setText("Nº");
        jPanel3.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(420, 100, -1, -1));
        jPanel3.add(edtIE, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 180, 140, -1));

        jLabel4.setText("IE:");
        jPanel3.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 180, -1, -1));
        jPanel3.add(edtCelular, new org.netbeans.lib.awtextra.AbsoluteConstraints(360, 250, 130, -1));

        jLabel10.setText("Celular:");
        jPanel3.add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 260, 60, -1));

        jLabel11.setText("Cidade:");
        jPanel3.add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 140, -1, -1));
        jPanel3.add(edtCodCidade, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 140, 50, -1));

        btnPesquisar.setText("P");
        btnPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPesquisarActionPerformed(evt);
            }
        });
        jPanel3.add(btnPesquisar, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 140, 40, -1));
        jPanel3.add(edtCidade, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 140, 190, -1));
        jPanel3.add(edtUF, new org.netbeans.lib.awtextra.AbsoluteConstraints(440, 140, 50, -1));

        jLabel12.setText("UF:");
        jPanel3.add(jLabel12, new org.netbeans.lib.awtextra.AbsoluteConstraints(410, 150, 30, -1));

        jPanel1.add(jPanel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 520, 390));

        jPanel2.setBorder(javax.swing.BorderFactory.createCompoundBorder(null, javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));
        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        btnGravar.setText("Salvar");
        btnGravar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGravarActionPerformed(evt);
            }
        });
        jPanel2.add(btnGravar, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 0, 70, 50));

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });
        jPanel2.add(btnCancelar, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 0, -1, 50));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 521, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 490, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-531)/2, (screenSize.height-483)/2, 531, 483);
    }// </editor-fold>//GEN-END:initComponents

    private void edtNoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_edtNoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_edtNoActionPerformed

    private void btnGravarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGravarActionPerformed
        // TODO add your handling code here:
        if (validar()) {

           
            dao.salvar(Long.parseLong(edtCodigo.getText()),
                    edtNome.getText(),
                    edtEndereco.getText(),
                    edtNo.getText(),
                    cidade,
                    edtTelefone.getText(),
                    edtObs.getText(),
                    Long.parseLong(edtCNPJ.getText()),
                    Long.parseLong(edtIE.getText()),
                    edtRepresentante.getText(),
                    edtTelefone.getText());
            this.dispose();

        }
    }//GEN-LAST:event_btnGravarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPesquisarActionPerformed
        // TODO add your handling code here:
        pesquisarCidade pesquisa = new pesquisarCidade(null, rootPaneCheckingEnabled);
        cidade = pesquisa.Seleciona();
        edtCodCidade.setText(cidade.getCod_cidade().toString());
        edtCidade.setText(cidade.getCidade().toString());
        edtUF.setText(cidade.getUf().toString());
    }//GEN-LAST:event_btnPesquisarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CadConvenio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CadConvenio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CadConvenio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CadConvenio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                CadConvenio dialog = new CadConvenio(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnGravar;
    private javax.swing.JButton btnPesquisar;
    private javax.swing.JTextField edtCNPJ;
    private javax.swing.JTextField edtCelular;
    private javax.swing.JTextField edtCidade;
    private javax.swing.JTextField edtCodCidade;
    private javax.swing.JTextField edtCodigo;
    private javax.swing.JTextField edtEndereco;
    private javax.swing.JTextField edtIE;
    private javax.swing.JTextField edtNo;
    private javax.swing.JTextField edtNome;
    private javax.swing.JTextArea edtObs;
    private javax.swing.JTextField edtRepresentante;
    private javax.swing.JTextField edtTelefone;
    private javax.swing.JTextField edtUF;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    public void alterar(Object obj) {
        Convenio convenio = (Convenio) obj;
        edtCodigo.setText(convenio.getCod_pessoa().toString());
        edtNome.setText(convenio.getNome().toString());
        edtEndereco.setText(convenio.getEndereco().toString());
        edtNo.setText(convenio.toString());
        edtIE.setText(convenio.toString());
        edtCNPJ.setText(convenio.toString());
        edtRepresentante.setText(convenio.getRepresentante());
        edtTelefone.setText(convenio.getTelefone().toString());
        edtCelular.setText(convenio.getCelular().toString());
        edtObs.setText(convenio.getObs().toString());
        edtCodCidade.setText(convenio.getCidade().getCod_cidade().toString());
        edtCidade.setText(convenio.getCidade().getCidade().toString());
        edtUF.setText(convenio.getCidade().getUf().toString());




    }

    public boolean validar() {
        if (edtNome.getText().equals("")) {
            new mensagem(null, true, "O Campo Nome é Obrigatório!").setVisible(true);
            return false;
        } else if (edtRepresentante.getText().equals("")) {
            new mensagem(null, true, "O Campo Representante é Obrigatório!").setVisible(true);
            return false;
        } else {
            if (edtCodigo.getText().equals("")) {
                edtCodigo.setText("0");
            }
            if (edtCNPJ.getText().equals("")) {
                edtCNPJ.setText("0");
            }
            if (edtIE.getText().equals("")) {
                edtIE.setText("0");
            }
            if (edtTelefone.getText().equals("")) {
                edtTelefone.setText("0");
            }
            return true;

        }


    }
}
